package control

import (
	"WeChatMGRA/modul"
	"WeChatMGRA/requests"
	"WeChatMGRA/server/smodul"
	"gitee.com/yutiandou/wdtools/wdlog"
)

//TextVerify ..当有人@了你，需要验证口令
func TextVerify(r * smodul.Robots,data * modul.ReceiveData)int{
	//thash := wdutil.MD5([]byte(text))
	headlen := len(`@`+r.NickName+` `)
	if (headlen+2)>=len(data.Content) {
		textVerify_again("default",r,data)
		return -1
	}
	if res:=textVerify_again(data.Content[headlen:],r,data);res>0{
		return res
	}else if res:=textVerify_again(data.Content[headlen+2:],r,data);res>0 {
		return  res
	}
	requests.SendText(r.Wid,data.FromGroup,"当前指令不识别，请@小助手发送如下内容进行操作：\n我是主播\n开始直播\n结束直播",false)
	wdlog.Debugln(data.Content)
	wdlog.Debugln(headlen,len(data.Content),data.Content[headlen:])
	return -1
}

func textVerify_again(text string,r * smodul.Robots,data * modul.ReceiveData)int{
	switch text {
	case "我是主播":
		if(r.WorkProgress>99){
			requests.SendText(r.Wid,data.FromGroup,"直播已经开始",false)
			return 1
		}
		//r.CurrentOwner = data.FromUser
		if r.CurrentGroup == "null"{
			r.CurrentGroup = data.FromGroup
		}
		if r.CurrentGroup == data.FromGroup {
			r.CurrentListWcid.Store(data.FromUser,true)
			requests.SendText(r.Wid,data.FromGroup,"主持人设置成功",false)
			smodul.NewRecordAndInsert(r.ID,r.NickName,data.Content,"机器人被主播认证")
		}
		return 1
	case "开始直播":
		if _,ok:=r.CurrentListWcid.Load(data.FromGroup);r.CurrentGroup==data.FromGroup&&ok{
			r.WorkProgress = 100
			requests.SendText(r.Wid,data.FromGroup,"小助手"+r.NickName+"开始持续为您转播",false)
			return 100
		}
	case "结束直播":
		if _,ok:=r.CurrentListWcid.Load(data.FromGroup);r.CurrentGroup==data.FromGroup&&ok{
			r.WorkProgress = 101
			requests.SendText(r.Wid,data.FromGroup,"感谢您的使用"+"\n——"+r.NickName,false)
			return 101 //转播结束
		}
	default:
		//requests.SendText(r.Wid,data.FromGroup,"当前指令不识别，请@小助手发送如下内容进行操作：\n为本群开始转播服务\n开启直播\n结束直播",false)
	}
	return -1
}